/*
Copyright 2010 Iain Surgey.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
#define KEYCODE_FORWARD			87
#define KEYCODE_BACK			83
#define KEYCODE_STRAFE_L		65
#define KEYCODE_STRAFE_R		68
#define KEYCODE_UP				81
#define KEYCODE_DOWN			90
#define KEYCODE_LOOK_LEFT		37
#define KEYCODE_LOOK_RIGHT		39
#define KEYCODE_LOOK_UP			38
#define KEYCODE_LOOK_DOWN		40
#define KEYCODE_B				66
#define KEYCODE_G				71
#define KEYCODE_T				84

#define KEYCODE_U				85
#define KEYCODE_H				72
#define KEYCODE_J				74
#define KEYCODE_K				75

#define KEYCODE_R				82

#define MOVE_SPEED 0.5
#define LOOK_SPEED 0.1

#define LERP_SPEED  150

#__body__

private var fwd : Bool;
private var bck : Bool;
private var slt : Bool;
private var srt : Bool;
private var up : Bool;
private var dn : Bool;
private var llt : Bool;
private var lrt : Bool;
private var lup : Bool;
private var ldn : Bool;

private var dx : Float;
private var dy : Float;
private var dz : Float;
private var dx2 : Float;
private var dy2 : Float;

private var curtime : Float;
private var lastime : Float;
private var tdiff : Float;
private var lerptime : Float;

private var cha : Bool;
private var dmov : Float;
private var dangle : Float;

private var frames : Int;

private var scrtxt : flash.text.TextField;
private var txformat : flash.text.TextFormat;
private var fpstime : Float;

private var ship : Bool;

private function InitInput() {
	fwd = false;
	bck = false;
	slt = false;
	srt = false;
	up = false;
	dn = false;
	llt = false;
	lrt = false;
	lup = false;
	ldn = false;
	
	ship = false;
	
	lastime = flash.Lib.getTimer();
	lerptime = fpstime = lastime;
	frames = 0;
	
	flash.Lib.current.stage.addEventListener( KeyboardEvent.KEY_DOWN, I_KeyDown );
	flash.Lib.current.stage.addEventListener( KeyboardEvent.KEY_UP, I_KeyUp );
	flash.Lib.current.stage.addEventListener( Event.ENTER_FRAME, I_NewFrame );
}


private function I_NewFrame ( e : Event ) {
	var moved : Bool;
	cha = false;
	
	dx = dy = dz = dx2 = dy2 = 0;
	curtime = flash.Lib.getTimer();
	tdiff = curtime-lastime;
	lastime = curtime;
	dmov = tdiff*MOVE_SPEED;
	dangle = tdiff*LOOK_SPEED;
	
	if (fwd) { dx -= dmov; }
	if (bck) { dx += dmov; }
	if (slt) { dy -= dmov; }
	if (srt) { dy += dmov; }
	if (up) { dz += dmov; }
	if (dn) { dz -= dmov; }
	if (llt) { dx2 += dangle; }
	if (lrt) { dx2 -= dangle; }
	if (lup) { dy2 -= dangle; }
	if (ldn) { dy2 += dangle; }	
	
	if (alias_visible) {
		if (curtime-lerptime > LERP_SPEED) {
			#{s_pmdl->frame = s_pmdl->frame + 1};
			lerptime += LERP_SPEED;
		}
		#{s_pmdl->backlerp = 1.0-((#[curtime-lerptime]#)/LERP_SPEED)};
	}
	
	if (I_MoveCam() || alias_visible) {
		R_RenderFrame();
		frames++;
		if (curtime-fpstime > 1000) {
			scrtxt.text = "hxQuake\nFPS: " + frames;
			scrtxt.setTextFormat(txformat);
			write("\nMove: W, A, S, D");
			write("Look: Arrow keys");
			frames = 0;
			fpstime = curtime;
		}
	}
}


private function I_KeyDown(e : KeyboardEvent) {
	//write("down: "+e.keyCode);
	switch (e.keyCode) {
		case KEYCODE_FORWARD:
			fwd = true;
		case KEYCODE_BACK:
			bck = true;
		case KEYCODE_STRAFE_R:
			slt = true;
		case KEYCODE_STRAFE_L:
			srt = true;
		case KEYCODE_UP:
			up = true;
		case KEYCODE_DOWN:
			dn = true;
		case KEYCODE_LOOK_LEFT:
			llt = true;
		case KEYCODE_LOOK_RIGHT:
			lrt = true;
		case KEYCODE_LOOK_UP:
			lup = true;
		case KEYCODE_LOOK_DOWN:
			ldn = true;
	}
	
}


private function I_KeyUp ( e : KeyboardEvent ) {
	//write("up: "+e.keyCode);	
	switch (e.keyCode) {
		case KEYCODE_FORWARD:
			fwd = false;
		case KEYCODE_BACK:
			bck = false;
		case KEYCODE_STRAFE_R:
			slt = false;
		case KEYCODE_STRAFE_L:
			srt = false;
		case KEYCODE_UP:
			up = false;
		case KEYCODE_DOWN:
			dn = false;
		case KEYCODE_LOOK_LEFT:
			llt = false;
		case KEYCODE_LOOK_RIGHT:
			lrt = false;
		case KEYCODE_LOOK_UP:
			lup = false;
		case KEYCODE_LOOK_DOWN:
			ldn = false;
	}
}


private inline function I_MoveCam () : Bool {
	cha = false;
	
	if (dx2 != 0) { 
		#{viewangles[1] = viewangles[1] + #[dx2]#};
		cha = true; 
	}
	
	if (dy2 != 0) { 
		#{viewangles[0] = viewangles[0] + #[dy2]#};
		cha = true; 
	}
	
	if (dx != 0 || dy != 0) {
		cha = true;
		#{vieworg[1] =  vieworg[1]
					#[- (Math.sin(]# viewangles[1] * #[Math.PI/180 )*dx)]# 
					#[+ (Math.cos(]# viewangles[1] * #[Math.PI/180 )*dy)]#};
		
		#{vieworg[0] =  vieworg[0]
					#[- (Math.cos(]# viewangles[1] * #[Math.PI/180 )*dx)]#
					#[- (Math.sin(]# viewangles[1] * #[Math.PI/180 )*dy)]#};
	}
	
	if (dz != 0) {
		#{vieworg[2] = vieworg[2] + #[dz]#};
		cha = true; 
	}
	
	return cha;
}


private inline function I_MoveAlias () : Bool {
	
	if (dx2 != 0) { 
		#{s_pmdl->angles[1] = s_pmdl->angles[1] + #[dx2]#};
		cha = true; 
	} else {
		
	}
	
	if (dx != 0 || dy != 0) {
		cha = true;
		#{s_pmdl->origin[1] = s_pmdl->origin[1] 
					#[- (Math.sin(]# (s_pmdl->angles[1]) * #[Math.PI/180 )*dx)]# 
					#[+ (Math.cos(]# (s_pmdl->angles[1]) * #[Math.PI/180 )*dy)]#};
		
		#{s_pmdl->origin[0] = s_pmdl->origin[0] 
					#[- (Math.sin(]# (s_pmdl->angles[1]) * #[Math.PI/180 )*dy)]#
					#[- (Math.cos(]# (s_pmdl->angles[1]) * #[Math.PI/180 )*dx)]#};
	}
	
	if (dz != 0) {
		#{s_pmdl->origin[2] = s_pmdl->origin[2] + #[dz]#};
		cha = true; 
	}
	
	return cha;
}


